package com.renwei.yunlong.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.WorkInformationActivity;
import com.renwei.yunlong.adapter.WorkRelateAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.WorkRelatedBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import butterknife.Bind;

/**
 * 工单信息  相关  页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class WorkRelatedFragment extends BaseLazyFragment implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    private WorkRelateAdapter adapter;
    private String requestId;
    private String outsourceFlag = "0";

    @SuppressLint("ValidFragment")
    public WorkRelatedFragment(String requestId, String outsourceFlag) {
        this.requestId = requestId;
        this.outsourceFlag = outsourceFlag;
    }

    public WorkRelatedFragment() {
    }

    @Override

    protected int getLayoutId() {
        return R.layout.fragment_work_related;
    }

    @Override
    public void onLazyLoad() {
        initView();
        initData();
    }

    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new WorkRelateAdapter(getContext());
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);

    }

    public void initData() {
        ServiceRequestManager.getManager().getWorkRelate(getContext(), StringUtils.value(outsourceFlag), requestId, this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        WorkRelatedBean ownerBean = new Gson().fromJson(result, WorkRelatedBean.class);
        if (ownerBean.getMessage().getCode() == 200) {
            if (ownerBean.getRows().size() == 0) {
                stateLayout.showEmptyView();
            } else {
                stateLayout.showContentView();
            }
            adapter.setData(ownerBean.getRows());
        }
    }

    @Override
    public void onException(int requestId, String code) {
        stateLayout.showErrorView();
    }

    @Override
    public void onItemClick(View view, int position) {
        WorkRelatedBean.Rows bean = adapter.getItem(position);
        WorkInformationActivity.OpenActivity(getContext(), bean.getRequestId() + "", bean.getServerRequestId(), bean.getOutsourceFlag());
    }
}
